﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Xml;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;


namespace AMS.Web.AssetQuery
{
    public partial class WorkBillInfoDetail : Page
    {
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private PageBuilder m_PageBuilder;
        BaseDataManager m_Help = new BaseDataManager();
        DataSet m_Ds;
        private string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("ClassName", this); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("TypeName", this); }
        }

        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                   return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
           
            if (TypeName.Length > 0)
                m_PageBuilder.SetPgListAttribute(pgList, ClassName, TypeName);
            else
                m_PageBuilder.SetPgListAttribute(pgList, ClassName);
            base.OnInit(e);
            pgList.Columns.Remove(0);
            pgList.Columns.Remove(0);
            pgList.ExportAllDataEnabled = false;
            pgList.ExportCurrentPageDataEnabled = false;
            pgList.PevButtomImgSrcEnable = "False";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            pgList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            if (!IsPostBack)
            {

                if (Request.QueryString["Id"] != null)
                {
                    string strId = Request.QueryString["Id"].Trim();
                    QueryCondition = " where c.Id ='" + strId + "'";
                }
                else if (Request.QueryString["WorkBillId"] != null)
                {
                    string strId = Request.QueryString["WorkBillId"].Trim();
                    QueryCondition = " where a.Id ='" + strId + "'";
                }
                  string sql =
                            "SELECT a.Id ,a.Code ,a.Theme ,(case a.TypeName when 'Consuming' then '领用' when 'Maintain' then '维护' when 'Borrow' then '借用' when 'Return' then '返还' when 'BackPosition' then '退仓' when 'Allocation' then '调拨' when 'Scrap' then '报废' when 'Damage' then '损毁' when 'CatValue' then '减值' when 'IsUsed' then '启用' when 'IsIdleness' then '停用' else '未知' end) TypeName ," +
                            "b.Name ,a.CreateDate   from AmsWorkBill_WoInfo a left outer join Frame_User b on a.SenderId = b.Id  left outer join AmsWorkBill_WoCommand c on a.Code = c.WoCode " +
                            QueryCondition;
                         DataTable dt = m_Help.GetDataTableBySql(sql);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            labelCode.Text = dt.Rows[0]["Code"].ToString();
                            labelTheme.Text = dt.Rows[0]["Theme"].ToString();
                            labelSender.Text = dt.Rows[0]["Name"].ToString();
                            labelTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                            labelCreateDate.Text = dt.Rows[0]["CreateDate"].ToString();
                            QueryCondition = " where 单据编号 ='" + dt.Rows[0]["Code"] + "'";
                            pgList.BindGrid();
                          
                        }
              }
           
        }

        private object BindList()
        {
            
            var acc = new BaseDataAccess();
            var sqlHelper = new SqlHelper();
            string sql = m_PageBuilder.GetPgListSqlValue(ClassName, TypeName);
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            sql += QueryCondition;
            acc.BeginTransaction();
            try
            {
                m_Ds = sqlHelper.ExecuteDataSet(acc.Session, sql);
            }
            finally
            {
                acc.CloseSession();
            }
         
           return m_Ds;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Window.Close();
        }
    }
}
